package com.aixin.project.servlet;

import com.aixin.project.data.*;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.util.RequestUtil;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-4-21
 * Time: 上午9:48
 * To change this template use File | Settings | File Templates.
 */

/**
 * 本servlet显示支付金额，订购点数信息，点击“支付”按钮调用支付宝接口进行支付
 */
public class PayServlet extends BaseServlet {
    @Override
    protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        System.out.println("begin");
        //获得course 订单信息，展示 （订单所属人安全验证）
        long coid = RequestUtil.getLong(request, "coid", -1);
        if (coid > 0) {
            AxSession axSession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
            if (axSession != null && axSession.getUserType().equals(UserType.USER)) {
                CourseOrder courseOrder = CourseOrderHandler.find(coid);
                if (courseOrder!=null && courseOrder.getUserid() == axSession.getUid()) { //判断是否属于该用户
                    Expert expert = ExpertHandler.find(courseOrder.getExportid());
                    Course course = CourseHandler.find(courseOrder.getCourseid());
                    ctx.put("courseOrder", courseOrder);
                    ctx.put("expert", expert);
                    ctx.put("course", course);
                    System.out.println("course.name:"+course.getTitle());
                    System.out.println("expert.name:"+expert.getName());
                }else{
                    ctx.put("ERROR","您无权操作此订单！");
                }
            } else {
                ctx.put("ERROR", "您还没有登录！");
            }
        } else {
            ctx.put("ERROR", "不存在此订单！");
        }
        System.out.println("end");
        return getTemplate("pay.vm");
    }
}
